package com.bleeth.fx.component;

import com.bleeth.fx.helper.AppHelper;
import com.bleeth.fx.helper.SStyleHelper;
import javafx.geometry.Pos;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import lombok.Builder;


/**
 * @author Sakura
 * @version 1.0
 * @date 2021-11-13 22:17
 **/
@Builder
public class MWindowBar {

    @Builder.Default
    private boolean close = true;

    @Builder.Default
    private boolean max = true;

    @Builder.Default
    private boolean mini = true;

    @Builder.Default
    private double height = 30;


    public Region make() {
        HBox box = new HBox();
        box.setPrefHeight(height);
        box.setAlignment(Pos.CENTER_RIGHT);

        if (mini) {
            Region miniIcon = MIcon.builder().icon("icon-search").build().make();
            SStyleHelper.node(miniIcon)
                    .addHoverStyle("-fx-background-color", "#dadada")
                    .addStyle("-fx-background-color", "#ffffff")
                    .apply();

            miniIcon.setOnMouseClicked(event -> {
                AppHelper.mainStage.setIconified(true);
            });
            box.getChildren().add(miniIcon);
        }
        if (max) {
            StackPane mmPane = new StackPane();
            Region maxIcon = MIcon.builder().icon("icon-remove").build().make();
            Region norIcon = MIcon.builder().icon("icon-view").build().make();

            maxIcon.setVisible(false);
            norIcon.setVisible(false);

            mmPane.getChildren().addAll(maxIcon, norIcon);
            mmPane.setPrefSize(maxIcon.getPrefWidth(), maxIcon.getPrefHeight());

            SStyleHelper.node(mmPane)
                    .addHoverStyle("-fx-background-color", "#dadada")
                    .addStyle("-fx-background-color", "#ffffff")
                    .apply();

            box.getChildren().add(mmPane);

            if (AppHelper.mainStage.isMaximized()) {
                norIcon.setVisible(true);
            } else {
                maxIcon.setVisible(true);
            }

            AppHelper.mainStage.maximizedProperty().addListener((observable, oldValue, newValue) -> {
                if (AppHelper.mainStage.isMaximized()) {
                    maxIcon.setVisible(false);
                    norIcon.setVisible(true);
                } else {
                    maxIcon.setVisible(true);
                    norIcon.setVisible(false);
                }
            });

            mmPane.setOnMouseClicked(event -> {
                AppHelper.mainStage.setMaximized(!AppHelper.mainStage.isMaximized());
            });
        }


        if (close) {
            Region closeIcon = MIcon.builder().icon("icon-close").build().make();
            SStyleHelper.node(closeIcon)
                    .addHoverStyle("-fx-background-color", "#e81123")
                    .addStyle("-fx-background-color", "#ffffff")
                    .apply();

            closeIcon.setOnMouseClicked(event -> {
                AppHelper.mainStage.close();
            });
            box.getChildren().add(closeIcon);
        }

        return box;
    }

}
